﻿using System.Collections.Generic;
using System.Web.Mvc;
using CodeCamp.Models.Home;

namespace CodeCamp.Controllers
{
  [HandleError]
  public class HomeController : Controller
  {
    public ActionResult Index( string eventName )
    {
      (new TransformEntities00ToEntities01()).Execute();

      int eventId = EventsHelper.GetEventId( eventName );
      var repository = new CodeCampDataRepository();
      var eventDisplayName = repository.GetEvent( eventId ).DisplayName;
      var eventSponsors = repository.GetSponsors( eventId );

      // assemble model
      var sponsors = new List<HomeIndexViewModel.Sponsor>();
      foreach ( var sponsor in eventSponsors )
      {
        var scaledImage = 
          SponsorsHelper.GetScaledImageSize( sponsor.ImageWidth, sponsor.ImageHeight );
        sponsors.Add(
          new HomeIndexViewModel.Sponsor
          {
            Name = sponsor.Name,
            Url = sponsor.Url,
            ImageUrl = sponsor.ImageUrl,
            ImageWidth = scaledImage.Width,
            ImageHeight = scaledImage.Height
          } );
      }
      sponsors.PermuteRandomly();

      var model = 
        new HomeIndexViewModel
        { 
          EventDisplayName = eventDisplayName,
          Sponsors = sponsors 
        };

      return View( model );
    }

    public ActionResult About( string eventName )
    {
      int eventId = EventsHelper.GetEventId( eventName );
      var repository = new CodeCampDataRepository();
      var eventDisplayName = repository.GetEvent( eventId ).DisplayName;

      var model =
        new HomeAboutViewModel
        {
          EventDisplayName = eventDisplayName
        };

      return View( model );
    }
  }
}
